Class Nur.MNISV3.Service.UserCenter Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// 根据userID查找默认病区科室
/// userId:用户Id，logonLocType：科室类型
/// w ##class(Nur.MNISV3.Service.UserCenter).getUserDefLoc(69,"W")
ClassMethod GetUserDefLoc(userId As %String, logonLocType As %String = "W") As %String
{
		n (userId,logonLocType)
		s userName=""
		s defLocId="",defLocDesc="",defLocType=""
		s defGroupId="",defGroupDesc=""
		
		s userObj=##class(User.SSUser).%OpenId(userId)
		i ($IsObject(userObj)){
			s userName=userObj.SSUSRName
			s defLocObj=userObj.SSUSRDefaultDeptDR
			s defGroupObj=userObj.SSUSRGroup
			i ($IsObject(defLocObj)){
				s defLocDesc=defLocObj.CTLOCDesc
				i defLocDesc["-" s defLocDesc=$p(defLocDesc,"-",2)
				s defLocType=defLocObj.CTLOCType
				s defLocId=defLocObj.%Id()
				d defLocObj.%Close()
			}
			i ($IsObject(defGroupObj)){
				s defGroupId=defGroupObj.%Id()
				i defGroupId'="" s defGroupDesc=$p(^SSU("SSGRP",defGroupId),"^",1) 
				d defGroupObj.%Close()
			}
			i (($g(defLocType)'="")&(logonLocType'="")&(defLocType'=logonLocType)){
				s defLocId=""
				s defLocDesc=""
				s defGroupId=""
				s defGroupDesc=""
			}
			d userObj.%Close()
		}
        
		q userName_"^"_defLocId_"^"_defLocDesc_"^"_defGroupId_"^"_defGroupDesc
}

/// 根据userID查找可以登录的病区科室
/// userId:用户Id，logonLocType：科室类型
/// w ##class(Nur.MNISV3.Service.UserCenter).getUserLoc(69,"W")
ClassMethod GetUserLoc(userId As %String, logonLocType As %String = "W") As %ListOfDataTypes
{
		n (userId,logonLocType)
		s defLocId="",userName="",defLocType=""
		s locJson=##class(%ListOfDataTypes).%New()
		s userObj=##class(User.SSUser).%OpenId(userId)
		i ($IsObject(userObj)){
			s userName=userObj.SSUSRName
			s defLocObj=userObj.SSUSRDefaultDeptDR
			s defGroupObj=userObj.SSUSRGroup
			i ($IsObject(defLocObj)){
				s defLocDesc=defLocObj.CTLOCDesc
				i defLocDesc["-" s defLocDesc=$p(defLocDesc,"-",2)
				s defLocType=defLocObj.CTLOCType
				s defLocId=defLocObj.%Id()
				d defLocObj.%Close()
			}
			i ($IsObject(defGroupObj)){
				s defGroupId=defGroupObj.%Id()
				i defGroupId'="" s defGroupDesc=$p(^SSU("SSGRP",defGroupId),"^",1) //by pan 20130924
				d defGroupObj.%Close()
			}
			i (($g(defLocType)'="")&(logonLocType'="")&(defLocType'=logonLocType)){
				s defLocId=""
				s defLocDesc=""
				s defGroupId=""
				s defGroupDesc=""
			}
			d userObj.%Close()
		}
       
		i (defLocId'=""){
	
			s locChildJson=##class(%ArrayOfDataTypes).%New()
			d locChildJson.SetAt(defLocId,"locId")
			d locChildJson.SetAt(defLocDesc_" "_defGroupDesc,"locDesc")  
			d locChildJson.SetAt(defGroupId,"groupId")
			d locChildJson.SetAt(defGroupDesc,"groupDesc")
			s defWardId=$o(^PAWARD(0,"WARD_LocationDR",defLocId,""))
			d locChildJson.SetAt(defWardId,"wardId")
			s locAndHospital=..GetWardLinkLocId(defWardId)
			s linkLoc=$P(locAndHospital,"^",1)
			s hospitalRowId=$P(locAndHospital,"^",2)
			d locChildJson.SetAt(linkLoc,"linkLoc")
			d locChildJson.SetAt(hospitalRowId,"hospitalRowId")
			
			d locJson.Insert(locChildJson)
		}
		s rs=##class(%ResultSet).%New("Nur.MNISV3.Service.UserCenter:LogonCtloc")
		d rs.Execute(userId)
		while(rs.Next()){
			s locId=rs.GetDataByName("locId")
			s groupId=rs.GetDataByName("groupId")
			s locType=$p($G(^CTLOC(locId)),"^",13)
			continue:(locType'="")&(logonLocType'="")&(logonLocType'=locType)
			s locDesc=$p($G(^CTLOC(locId)),"^",2)
			i locDesc["-" s locDesc=$p(locDesc,"-",2)
			i groupId'="" s groupDesc=$p(^SSU("SSGRP",groupId),"^",1) 
			continue:groupId=""
			s locChildJson=##class(%ArrayOfDataTypes).%New()
			d locChildJson.SetAt(locId,"locId")
			d locChildJson.SetAt(locDesc_"  "_groupDesc,"locDesc") 
			d locChildJson.SetAt(groupId,"groupId")
			d locChildJson.SetAt($g(groupDesc),"groupDesc")
			s wardId=$o(^PAWARD(0,"WARD_LocationDR",locId,"")) 
			d locChildJson.SetAt(wardId,"wardId")
			s locAndHospital=..GetWardLinkLocId(wardId)
			s linkLoc=$P(locAndHospital,"^",1)
			s hospitalRowId=$P(locAndHospital,"^",2)
			d locChildJson.SetAt(linkLoc,"linkLoc")
			d locChildJson.SetAt(hospitalRowId,"hospitalRowId")
			
			d locJson.Insert(locChildJson)
		}
		q locJson
}

// d ##class(%ResultSet).RunQuery("Nur.MNIS.Service.UserCenter","logonCtloc",69)

/// 查找病人可以登录的科室
Query LogonCtloc(userId As %String) As %Query(ROWSPEC = "locId:%String,groupId:%String")
{
}

ClassMethod LogonCtlocExecute(ByRef qHandle As %Binary, userId As %String) As %Status
{
	Set repid=$I(^CacheTemp)
 	s ind=1
    s chl=""  f  s chl=$O(^SSU("SSUSR",userId,"OTHLL",chl)) q:chl=""  d
    .s locId=$P(^SSU("SSUSR",userId,"OTHLL",chl),"^",1)
    .s groupId=$P(^SSU("SSUSR",userId,"OTHLL",chl),"^",2)
    .s endDate=$P(^SSU("SSUSR",userId,"OTHLL",chl),"^",5)
	.q:(endDate'="")&&(endDate'>+$H)
    .s locDesc=$p($G(^CTLOC(locId)),"^",2)
    .q:locDesc=""
	.q:(locDesc["停用")
    .s activeFlag=$p($g(^CTLOC(locId)),"^",14)
	.q:(activeFlag="N")
    .;s tm(locDesc)=locId_"^"_groupId  //by pan 20130924
    .s tm(locId_"^"_groupId)=locId_"^"_groupId  //
    s locId=""  f  s locId=$O(tm(locId)) q:locId=""  d
    .;b ;01
    .s id=$p(tm(locId),"^",1)
    .s groupId=$p(tm(locId),"^",2)
    .d OutRowlogonCtloc
    Set qHandle=$lb(0,repid,0)
	Quit $$$OK
  
OutRowlogonCtloc
	set Data=$lb(id,groupId)
 	Set ^CacheTemp(repid,ind)=Data
 	Set ind=ind+1
	quit
}

ClassMethod LogonCtlocFetch(ByRef qHandle As %Binary, ByRef Row As %List, ByRef AtEnd As %Integer = 0) As %Status [ PlaceAfter = LogonCtlocExecute ]
{
	Set AtEnd=$LIST(qHandle,1)
 	Set repid=$LIST(qHandle,2)
 	Set ind=$LIST(qHandle,3)
 	Set ind=$o(^CacheTemp(repid,ind))
 	If ind="" {				// if there are no more rows, finish fetching
 		Set AtEnd=1
 		Set Row=""
 	}
 	Else      {			
 		Set Row=^CacheTemp(repid,ind)
 	}
 	s qHandle=$lb(AtEnd,repid,ind)
	Quit $$$OK
}

ClassMethod LogonCtlocClose(ByRef qHandle As %Binary) As %Status [ PlaceAfter = LogonCtlocExecute ]
{
	Set repid=$LIST(qHandle,2)
 	Kill ^CacheTemp(repid)
 	Quit $$$OK
}

/// 获取病区关联科室及医院ID
/// Creator JYW 
/// Date 2018-08-08
/// Input wardId 病区Id
/// OutPut 
///  w ##class(Nur.MNISV3.Service.UserCenter).GetWardLinkLocId(5)
ClassMethod GetWardLinkLocId(wardId)
{
	n (wardId)
	q:(wardId="") ""
    s wardCtlocId=$p($g(^PAWARD(wardId)),"^",5)
    q:wardCtlocId="" ""
    s CTLocId=..GetLinkLocId(wardCtlocId)
    if CTLocId'="" s HospitalRowId=$p($G(^CTLOC(+CTLocId)),"^",22)
    
    i $g(HospitalRowId)="" s HospitalRowId="0"
    i (+HospitalRowId=0) s HospitalRowId=##class(Nur.MNISV3.Service.Session).GetWardHospID(wardId) // EH
    q +CTLocId_"^"_HospitalRowId
}

ClassMethod GetLinkLocId(locId)
{
	n (locId)
    q:locId="" ""
    s linkSub=0,ctlocIdStr=""
    f  s linkSub=$o(^CTLOC(locId,"LINK",linkSub)) q:linkSub=""  d
    	.i ctlocIdStr'="" s ctlocIdStr=ctlocIdStr_"^"
    	.s ctlocIdStr=ctlocIdStr_^CTLOC(locId,"LINK",linkSub)
	q ctlocIdStr
}

}
